﻿// ==================================================================================================
// Copyright(C) 2010 NCS Pte. Ltd. All rights reserved.
//
// SYSTEM NAME	    : iConnect .Net Application Framework
// COMPONENT ID	    : NCS.IConnect.Core
// COMPONENT DESC   : 
//
// CREATED DATE/BY  : 16 Nov 2011 / Jiang Jin Nan
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================

using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.Unity.Utility;
using NCS.IConnect.Core.Properties;
using System;
using System.Configuration;
using System.Reflection;

namespace NCS.IConnect.Core.IoC
{
    /// <summary>
    /// This <see cref="IMatchingRule"/> is used to apply call handlers to all sub classes of the given base class.
    /// </summary>
    public class BaseTypeMatchingRule: IMatchingRule
    {
        /// <summary>
        /// Gets the type of the base.
        /// </summary>
        /// <value>
        /// The type of the base.
        /// </value>
        public Type BaseType { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseTypeMatchingRule"/> class.
        /// </summary>
        /// <param name="baseTypeAssemblyQualifiedName">Name of the base type assembly qualified.</param>
        public BaseTypeMatchingRule(string baseTypeAssemblyQualifiedName)
        {
            Guard.ArgumentNotNullOrEmpty(baseTypeAssemblyQualifiedName, "baseTypeAssemblyQualifiedName");
            Type baseType = Type.GetType(baseTypeAssemblyQualifiedName);
            if (null == baseType)
            {
                throw new ConfigurationErrorsException(ResourceUtility.Format(Resources.ExceptionInvalidTypeAssemblyQualifiedName, baseTypeAssemblyQualifiedName));
            }
            this.BaseType = baseType;
        }

        /// <summary>
        /// Matcheses the specified member.
        /// </summary>
        /// <param name="member">The member.</param>
        /// <returns>A <see cref="bool"/> flag indicating whether to match the given method.</returns>
        public bool Matches(MethodBase member)
        {
            return this.BaseType.IsAssignableFrom(member.ReflectedType);
        }
    }
}
